package cn.itsource.order.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageList;
import cn.itsource.order.domain.AdoptOrder;
import cn.itsource.order.query.AdoptOrderQuery;
import cn.itsource.order.service.IAdoptOrderService;

import cn.itsource.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {
    @Autowired
    private IAdoptOrderService adoptOrderService;

    // 用户下单
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request) {
        // params是前端传过来的order，以map接收
        try {
            // 拿到登录用户信息
            Logininfo info = LoginContext.getLoginInfo(request);
            String payData = adoptOrderService.submit(params, info.getId());
            return AjaxResult.me().setResultObj(payData);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("========"+e.getMessage());
        }

    }

    // 管理员分页查询订单列表
    @PostMapping("/admin")
    public PageList<AdoptOrder> admin(@RequestBody AdoptOrderQuery adoptOrderQuery, HttpServletRequest request){
        Logininfo info = LoginContext.getLoginInfo(request);
        PageList<AdoptOrder> pageList = adoptOrderService.admin(adoptOrderQuery,info.getId());
        return pageList;
    }



    @PostMapping("/user")
    public PageList<AdoptOrder> user(@RequestBody AdoptOrderQuery adoptOrderQuery, HttpServletRequest request){
        Logininfo info = LoginContext.getLoginInfo(request);
        PageList<AdoptOrder> pageList = adoptOrderService.user(adoptOrderQuery,info.getId());
        return pageList;
    }




    @DeleteMapping("/{id}")  /*@RequestBody  接收请求体localhost:8080/adoptOrder/4*/
    @ApiOperation(value = "删除订单",notes = "根据ID删除订单")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            adoptOrderService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    // 查询详情
    @GetMapping("/{id}")
    public AdoptOrder loadById(@PathVariable("id") Long id) {
        AdoptOrder one = adoptOrderService.findOne(id);
        return one;
    }



}
